<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center
    width="800px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="sku">
          <el-input v-model="form.sku" placeholder="sku" class="w-full" />
        </el-form-item>
        <el-form-item label="活动类型" prop="promotion_config_id"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.promotion_config_id" placeholder="请选择" :options="options.typeOptions" filterable
            clearable class="w-full" />
        </el-form-item>
        <el-form-item label="国家" prop="country" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.country" placeholder="请选择" :options="options.country" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="备货仓库" prop="warehouse_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.warehouse_id" placeholder="请选择" type="warehouseList" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="运输方式" prop="transport_type"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.transport_type" placeholder="请选择" :options="options.transport_type" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="需求数量" prop="demand_num" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input-number v-model="form.demand_num" controls-position="right" :min="0" :step="1" :precision="0"
            class="w-full" />
        </el-form-item>
        <el-form-item label="需求到货日期" prop="demand_delivery_time"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-date-picker v-model="form.demand_delivery_time" placeholder="日期选择" type="date" value-format="YYYY-MM-DD"
            class="w-full" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" placeholder="备注" type="textarea" :autosize="{ minRows: 2, maxRows: 5 }" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick, reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (pd_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  formRef?.value?.resetFields()
  loadings = reactive({})
  visible.value = true
  const res = await api.detail({ pd_id })
  form.value = res.data
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  if (!await formRef.validate()) return
  loadings.create = true
  api.edit(form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>